<?php

namespace app\admin\logic;

use app\admin\model\Community;
use app\admin\model\Form3;
use app\admin\model\Form6;
use app\common\basics\Logic;
use EasyAdmin\upload\Uploadfile;
use jianyan\excel\Excel;
use think\App;
use think\Exception;

/**
 * Form6逻辑层
 */
class Form6Logic extends Logic
{
    /**
     * 导入发热人员信息
     * @param array $post 数据
     * @return bool
     */
    public static function import(array $post)
    {
        try {
            $uploadConfig = sysconfig('upload');
            $upload = Uploadfile::instance()
                ->setUploadType($post['upload_type'])
                ->setUploadConfig($uploadConfig)
                ->setFile($post['file'])
                ->save();
            if ($upload['save'] <> true) throw new Exception($upload['msg']);

            $address = parse_url($upload['url']);
            $path = public_path() . trim($address['path'],'/');
            $data = Excel::import($path, 3);
            if (empty($data)) throw new Exception('模板的数据行不能为空');

            $arr = [];
            foreach ($data as $v) {
                $res = [
                    'name'              =>  $v[0],
                    'id_card'           =>  $v[1],
                    'mobile'            =>  $v[2],
                    'living'            =>  $v[3],
                    'out_time'          =>  $v[4],
                    'community'         =>  Community::getNameById($v[5]),
                ];

                if (empty($res['id_card'])) continue;

                $arr[] = $res;
            }

            (new Form6())->saveAll($arr);
            return true;
        } catch (\Exception $e) {
            self::$error = $e->getMessage();
            return false;
        }
    }
}